package com.xiaoshuidi.cloud.module.rooms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomdevice.vo.RoomDevicePageVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomdevice.vo.RoomDeviceVO;
import com.xiaoshuidi.cloud.module.rooms.enums.DeviceTypeEnum;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomDeviceMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouse;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomSdiotDeviceMapping;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomHouseRepository;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomSdiotDeviceMappingRepository;
import com.xiaoshuidi.cloud.module.rooms.service.RoomDeviceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class RoomDeviceServiceImpl extends ServiceImpl<RoomDeviceMapper, RoomHouse> implements RoomDeviceService {

    private final RoomHouseRepository roomHouseRepository;
    private final RoomSdiotDeviceMappingRepository roomSdiotDeviceMappingRepository;

    @Override
    public PageResult<RoomDeviceVO> pageList(Integer pageSize, Integer pageNum, Long apartmentId, String area, String buildingNo, String unitNo, String houseNo, Boolean isConcentrated) {
        Page<RoomDevicePageVO> page = roomHouseRepository.devicePage(apartmentId, area, buildingNo, unitNo, houseNo, isConcentrated, pageNum, pageSize);
        List<RoomDevicePageVO> records = page.getRecords();
        List<RoomDeviceVO> roomDeviceVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(records)) {
            List<Long> roomIds = records.stream().map(s -> s.getId()).collect(Collectors.toList());
            List<RoomSdiotDeviceMapping> mappingList = roomSdiotDeviceMappingRepository.listByRoomIds(roomIds);
            Map<Long, List<RoomSdiotDeviceMapping>> group = mappingList.stream().collect(Collectors.groupingBy(RoomSdiotDeviceMapping::getRoomHouseId));
            roomDeviceVoList = records.stream().map(record -> {
                String area1 = record.getArea();
                String buildingNo1 = record.getBuildingNo();
                String unitNo1 = record.getUnitNo();
                String houseNo1 = record.getName();
                String finalName;
                if (isConcentrated) {
                    finalName = area1 + "小区" + buildingNo1 + "栋" + houseNo1;
                } else {
                    finalName = area1 + buildingNo1 + "栋" + unitNo1 + "单元" + houseNo1;
                }

                RoomDeviceVO roomDeviceVO = new RoomDeviceVO();
                List<RoomSdiotDeviceMapping> mappings = group.get(record.getId());
                if (CollectionUtil.isNotEmpty(mappings)) {
                    mappings.forEach(mapping -> {
                        String deviceType = mapping.getDeviceType();
                        if (DeviceTypeEnum.DOORLOCK.getName().equals(deviceType)) {
                            roomDeviceVO.setDoorLockDeviceNo(mapping.getDeviceThirdNo());
                        } else if (DeviceTypeEnum.AMMETER.getName().equals(deviceType)) {
                            roomDeviceVO.setAmmeterDeviceNo(mapping.getDeviceThirdNo());
                        } else if (DeviceTypeEnum.COLDWATER.getName().equals(deviceType)) {
                            roomDeviceVO.setColdWaterDeviceNo(mapping.getDeviceThirdNo());
                        } else if (DeviceTypeEnum.HOTWATER.getName().equals(deviceType)) {
                            roomDeviceVO.setHotWaterDeviceNo(mapping.getDeviceThirdNo());
                        }
                    });
                }
                roomDeviceVO.setRoomName(finalName);
                roomDeviceVO.setRoomId(record.getId());
                return roomDeviceVO;
            }).collect(Collectors.toList());
        }
        PageResult<RoomDeviceVO> roomDeviceVOPageResult = new PageResult<>();
        roomDeviceVOPageResult.setList(roomDeviceVoList);
        roomDeviceVOPageResult.setTotal(page.getTotal());
        return roomDeviceVOPageResult;
    }

}
